tywx.AdManager = {
    adIconBtn: null,
    adInfoList: [],
    currentAdInfo: null,
    currentWebPage: null,
    retryTimes: 3,
    _hasAD: !1,
    hasAd: function() {
        return this._hasAD;
    },
    requestADInfo: function() {
        if (tywx.IsWechatPlatform()) {
            this.retryTimes--;
            var t = {}, n = new Date().getTime();
            t.act = "api.getCrossConfig", t.time = n, t.game_mark = "8-20055";
            var e = this.getConfigSignStr(t), r = [];
            for (var i in t) r.push(i + "=" + t[i]);
            r.push("sign=" + e);
            var o = tywx.SystemInfo.shareManagerUrl + "?" + r.join("&"), c = this;
            wx.request({
                url: o,
                method: "GET",
                success: function(t) {
                    if (200 == t.statusCode) {
                        var n = t.data;
                        if (console.log("RET:" + JSON.stringify(n)), c.adInfoList = [], n.retmsg) for (var e in n.retmsg) c.adInfoList.push(n.retmsg[e]);
                        c.adInfoList.length > 0 && (c._hasAD = !0), c.retryTimes = 3;
                    }
                },
                fail: function(t) {
                    c.retryTimes > 0 ? c.requestADInfo() : c.retryTimes = 0;
                }
            });
        }
    },
    showAd: function(t) {
        if (this.genRandomFirstAdInfo(), this.currentAdInfo) if (this.adIconBtn) this.adIconBtn.active = !0; else {
            var n = this;
            cc.loader.loadRes("prefabs/adNode", function(e, r) {
                var i = cc.instantiate(r);
                i.position = cc.p(t.x, t.y), n.adIconBtn = i, cc.game.addPersistRootNode(i), cc.loader.load({
                    url: n.currentAdInfo.icon_url
                }, function(t, e) {
                    t || (i.getChildByName("adIcon").getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(e), 
                    i.getChildByName("adButton").on("click", function() {
                        tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickFirstAd, [ n.currentAdInfo.icon_url, n.currentAdInfo.icon_name ]), 
                        n.onClickAdIconBtn();
                    }));
                });
            });
        }
    },
    showAd2: function() {
        return this.genRandomFirstAdInfo(), this.currentAdInfo ? this.currentAdInfo.icon_url : null;
    },
    click: function(t) {
        var n = this;
        if (tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickFirstAd, [ n.currentAdInfo.icon_url, n.currentAdInfo.icon_name ]), 
        n.onClickAdIconBtn(), this.currentWebPage) return this.resetBtnIcon2();
        t && t(!1);
    },
    genRandomFirstAdInfo: function() {
        var t = this;
        if (0 != this.adInfoList.length) {
            var n = [ 0 ];
            for (var e in this.adInfoList) n.push(parseInt(this.adInfoList[e].icon_weight));
            n.sort(function(t, n) {
                return t > n;
            });
            var r = 0;
            n.forEach(function(t) {
                r += t;
            });
            for (var i = parseInt(1e4 * Math.random()) % (r + 1), o = 0, c = 0, e = 0; e < n.length - 1; e++) if ((o += n[e]) < i && o + n[e + 1] >= i) {
                c = e + 1;
                break;
            }
            var a = n[c];
            this.adInfoList.forEach(function(n) {
                n.icon_weight == a.toString() && (t.currentAdInfo = n);
            });
        }
    },
    genRandomSecondAdInfo: function() {
        var t = this, n = this.currentAdInfo.webpages;
        if (0 != n.length) {
            var e = [ 0 ];
            for (var r in n) e.push(parseInt(n[r].webpage_weight));
            e.sort(function(t, n) {
                return t > n;
            });
            var i = 0;
            e.forEach(function(t) {
                i += t;
            });
            for (var o = parseInt(1e4 * Math.random()) % (i + 1), c = 0, a = 0, r = 0; r < e.length - 1; r++) if ((c += e[r]) < o && c + e[r + 1] >= o) {
                a = r + 1;
                break;
            }
            var s = e[a];
            n.forEach(function(n) {
                n.webpage_weight.toString() == s.toString() && (t.currentWebPage = n);
            }), console.log("this.currentWebPage" + JSON.stringify(this.currentWebPage));
        }
    },
    hideAd: function() {
        this.adIconBtn && (this.adIconBtn.active = !1);
    },
    onClickAdIconBtn: function() {
        if (this.genRandomSecondAdInfo(), this.currentWebPage) {
            var t = this;
            tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeClickFirstAd, [ t.currentWebPage.webpage_url, t.currentAdInfo.config_id ]), 
            tywx.IsWechatPlatform() && wx.previewImage({
                current: [ t.currentWebPage.webpage_url ],
                urls: [ t.currentWebPage.webpage_url ],
                success: function(t) {},
                fail: function(t) {}
            });
        }
    },
    resetBtnIcon: function() {
        var t = this;
        this.genRandomFirstAdInfo(), t.adIconBtn && t.currentAdInfo.icon_url && cc.loader.load({
            url: t.currentAdInfo.icon_url
        }, function(n, e) {
            n || (t.adIconBtn.getChildByName("adIcon").getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(e));
        });
    },
    resetBtnIcon2: function() {
        this.genRandomFirstAdInfo();
    },
    getConfigSignStr: function(t) {
        for (var n = Object.keys(t).sort(), e = "", r = 0; r < n.length; r++) {
            var i = n[r];
            "act" != i && "sign" != i && (e += i + "=" + t[i]);
        }
        return tywx.hex_md5("market.tuyoo.com-api-" + e + "-market.tuyoo-api") || "";
    }
};